package com.baihan.angus.app.game.busshow.ui.user.upgrade.store;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.baihan.angus.app.game.basic.base.view_model.BaseViewModel;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.basic.utils.net.ApiObserver;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.LoginResponse;
import com.baihan.angus.app.game.common.bean.response.UpgradeResponse;
import com.baihan.angus.app.game.common.bean.response.UploadResponse;
import com.baihan.angus.app.game.common.bean.response.VerifyDetailResponse;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.data_center.remote.RemoteDataMng;

import java.io.File;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.baihan.angus.app.game.busshow.ui.user.upgrade.store.StoreSecondActivity.STORE_TAG_BACK;
import static com.baihan.angus.app.game.busshow.ui.user.upgrade.store.StoreSecondActivity.STORE_TAG_FRONT;
import static com.baihan.angus.app.game.busshow.ui.user.upgrade.store.StoreSecondActivity.STORE_TAG_LICENSE;

public class StoreSecondVM extends BaseViewModel {

    public StoreSecondVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofFrontUrl = new ObservableField<>("");
    public ObservableField<String> ofBackUrl = new ObservableField<>("");
    public ObservableField<String> ofLicenseUrl = new ObservableField<>("");

    public MutableLiveData<UpgradeResponse.Data> mldUpgrade = new MutableLiveData<>();
    public MutableLiveData<UpgradeResponse.Data> mldUpdateUpgrade = new MutableLiveData<>();
    public MutableLiveData<VerifyDetailResponse.Data> mldDetail = new MutableLiveData<>();

    public void getVerifyDetail(String id, String user) {
        showLoading("getVerifyDetail");
        RemoteDataMng.getInstance().getVerifyDetail(id, user, mLifecycleProvider.bindToLifecycle(), new ApiObserver<VerifyDetailResponse>() {
            @Override
            public void onSuccess(VerifyDetailResponse verifyDetailResponse) {
                hideLoading("getVerifyDetail");
                mldDetail.setValue(verifyDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getVerifyDetail");
                mldDetail.setValue(null);
                Tooast.warning(msg);
            }
        });
    }

    public void upgrade(Map<String, Object> pMap) {
        showLoading("upgrade");
        RemoteDataMng.getInstance().upgrade(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<UpgradeResponse>() {
            @Override
            public void onSuccess(UpgradeResponse upgradeResponse) {
                hideLoading("upgrade");
                mldUpgrade.setValue(upgradeResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("upgrade");
                mldUpgrade.setValue(null);
                Tooast.warning(msg);
            }
        });
    }

    public void updateUpgrade(String id, Map<String, Object> pMap) {
        showLoading("mldUpdateUpgrade");
        RemoteDataMng.getInstance().updateUpgrade(id, pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<UpgradeResponse>() {
            @Override
            public void onSuccess(UpgradeResponse upgradeResponse) {
                hideLoading("mldUpdateUpgrade");
                mldUpdateUpgrade.setValue(upgradeResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("mldUpdateUpgrade");
                mldUpdateUpgrade.setValue(null);
                Tooast.warning(msg);
            }
        });
    }

    public void uploadFile(File file, String tag) {
        showLoading("uploadFile");
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
        RemoteDataMng.getInstance().uploadFile(filePart, mLifecycleProvider.bindToLifecycle(), new ApiObserver<UploadResponse>() {
            @Override
            public void onSuccess(UploadResponse uploadResponse) {
                hideLoading("uploadFile");
                Tooast.warning(getApplication().getString(R.string.uploadSuccess));
                if (tag.equals(STORE_TAG_FRONT)) {
                    ofFrontUrl.set(uploadResponse.getData().getUrl());
                } else if (tag.equals(STORE_TAG_BACK)) {
                    ofBackUrl.set(uploadResponse.getData().getUrl());
                } else if (tag.equals(STORE_TAG_LICENSE)) {
                    ofLicenseUrl.set(uploadResponse.getData().getUrl());
                }
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("uploadFile");
                Tooast.warning(getApplication().getString(R.string.uploadFail));
            }
        });
    }

    public void getUserInfo() {
        showLoading("getUserInfo");
        RemoteDataMng.getInstance().getUserInfo( mLifecycleProvider.bindToLifecycle(), new ApiObserver<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse loginResponse) {
                hideLoading("getUserInfo");
                UserConfig.getUserConfig().saveUser(loginResponse.getData().getUserinfo());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getUserInfo");
                Tooast.warning(msg);
            }
        });
    }
}
